package com.smdk.dsminio.interceptors;

import cn.hutool.log.StaticLog;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smdk.dsminio.annotation.LoginRequired;
import com.smdk.dsminio.entity.DsminioUserInfo;
import com.smdk.dsminio.response.InterceptorReturnInfo;
import com.smdk.dsminio.utils.CookieUtil;
import com.smdk.dsminio.utils.GetIPAddress;
import com.smdk.dsminio.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Component
public class AuthInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    RestTemplate restTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        //Token令牌
        String accessToken = null;
        Map<String, Object> resultMap = new HashMap<>();
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        //获取请求中有没有带此注解类
        LoginRequired methodAnnotation = handlerMethod.getMethodAnnotation(LoginRequired.class);
        if (methodAnnotation == null) {
            return true;
        }
        //先获得用户cookie中关于用户的身份token有四种情况1.登录过就获取浏览器的cookie值 2 直接传递过来的token
        String oldToken = CookieUtil.getCookieValue(request, "accessToken", true);
        if (StringUtil.isNotNull(oldToken)) {
            accessToken = oldToken;
        }
        //获取登录时传过来的token值
        String newToken = request.getParameter("accessToken");
        if (StringUtil.isNotNull(newToken)) {
            accessToken = oldToken;
        }
        // 通过远程ws请求认证中心，验证token
        if (!StringUtil.isNotNull(accessToken) && methodAnnotation.isSuccess()) {
            StaticLog.info("无法获取到AccessToken信息");
            resultMap.put("returnCode", 400);
            resultMap.put("msg", "未登录！");
            resultMap.put("data", "request login info is null");
            InterceptorReturnInfo.response(response, JSONObject.toJSONString(resultMap));
            return false;
        }
        //开始像JWT认证中心认证AccessToken信息
        String requestUrl = "http://127.0.0.1:9094/verify?verifyAccessToken=" + accessToken;
        //返回信息
        String successJSON = restTemplate.getForObject(requestUrl, String.class);

        DsminioUserInfo dsminioUserInfo = JSON.parseObject(successJSON, DsminioUserInfo.class);
        //如果不为空并且是有传过来的success字符串
        if (dsminioUserInfo != null && dsminioUserInfo.getUserId() != null) {
            // 重新更新cookie的过期时间
            CookieUtil.setCookie(request, response, "accessToken", accessToken, 30 * 60, true);
            return true;
        } else {
            resultMap.put("returnCode", 401);
            resultMap.put("msg", "登录过期！");
            resultMap.put("data", "request login info time ex");
            InterceptorReturnInfo.response(response, JSONObject.toJSONString(resultMap));
            return false;
        }

    }
}
